package com.chagee.store.operation.server.service.biz;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.chagee.base.utils.PaginationData;
import com.chagee.base.utils.SoaResponse;
import com.chagee.base.utils.SoaUtil;
import com.chagee.personnel.api.event.bo.DeptBO;
import com.chagee.personnel.api.event.enums.ChageeDeptTagEnum;
import com.chagee.personnel.api.event.enums.ChageeDeptTypeEnum;
import com.chagee.personnel.api.event.enums.OrgQueryReq;
import com.chagee.personnel.api.event.request.*;
import com.chagee.personnel.api.event.response.*;
import com.chagee.personnel.api.event.service.ChageeDeptStoreApiService;
import com.chagee.personnel.api.event.service.DeptApiService;
import com.chagee.personnel.api.event.service.dept.ChageeOrgReadApiService;
import com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 人事相关能力
 */

@Service
@Slf4j
public class PersonalBizService {

	@Resource
	private ChageeEmpReadApiService chageeEmpReadApiService;

	@Resource
	private ChageeOrgReadApiService chageeOrgReadApiService;

	@Resource
	private DeptApiService deptApiService;

	@Resource
	private ChageeDeptStoreApiService chageeDeptStoreApiService;

	private static final EmployeeResp EMPTY_USER = new EmployeeResp();

	private static final DeptBO EMPTY_DEPT = new DeptBO();

	private static final Integer DEFAULT_PAGE_NUM = 1;

	private static final Integer DEFAULT_PAGE_SIZE = 1000;

	private static final String SERVICE_GROUP_TAG = "SERVICE_GROUP";

	public List<FindLenderByStoreNoResp> getLeaderInfoByStoreNo(String StoreNo,String postName){
		FindLenderByStoreNoReq request = new FindLenderByStoreNoReq();
		request.setStoreNo(Collections.singletonList(StoreNo));
		request.setPostName(postName);
		try {
			log.info("StoreRemovalThirdService#getLeaderInfoByStoreNo,request:{}", JSONObject.toJSONString(request));
			SoaResponse<List<FindLenderByStoreNoResp>, Void> response = chageeEmpReadApiService.getLeaderInfoByStoreNo(request);
			List<FindLenderByStoreNoResp> lenderList = SoaUtil.unpack(response);
			return lenderList;
		} catch (Exception e) {
			log.error("StoreRemovalThirdService#getLeaderInfoByStoreNo,request:{},error:{}", JSONObject.toJSONString(request),e);
			return new ArrayList<>();
		}
	}

	public StoreAndChildCompanyOfUserResp getChildCompanyOfUser(String userId){
		SoaResponse<StoreAndChildCompanyOfUserResp, Void> storeAndChildCompanyOfUser;
		StoreAndChildCompanyOfUserReq req = new StoreAndChildCompanyOfUserReq();
		req.setUserId(userId);
		try {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChildCompanyOfUser_request：{}",req);
			storeAndChildCompanyOfUser = chageeOrgReadApiService.getStoreAndChildCompanyOfUser(req);
		} catch (Exception e) {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChildCompanyOfUser_throw excepton." + e.getMessage());
			return null;
		}
		if (!SoaUtil.isSucc(storeAndChildCompanyOfUser) || Objects.isNull(storeAndChildCompanyOfUser.getResponseVo())) {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChildCompanyOfUser_respone:{}", JSON.toJSONString(storeAndChildCompanyOfUser));
			return null;
		}
		return storeAndChildCompanyOfUser.getResponseVo();
	}

	/**
	 * 查询子公司列表
	 * @return list 分公司三方打烊负责人
	 */
	public List<UserOfRolePageRequestResp> getUserOfRolePage(String roleAlias) {
		UserOfRolePageRequest request = new UserOfRolePageRequest();
		request.setRoleAlias(roleAlias);
		request.setPageNum(DEFAULT_PAGE_NUM);
		request.setPageSize(DEFAULT_PAGE_SIZE);
		SoaResponse<PaginationData<UserOfRolePageRequestResp>, Void> userOfRolePage;
		try {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getUserOfRolePage_request：{}",request);
			userOfRolePage = chageeEmpReadApiService.getUserOfRolePage(request);
		} catch (Exception e) {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getUserOfRolePage_throw excepton." + e.getMessage());
			return Lists.newArrayList();
		}
		if (!SoaUtil.isSucc(userOfRolePage) || Objects.isNull(userOfRolePage.getResponseVo()) ||
			Objects.isNull(userOfRolePage.getResponseVo().getPageList())) {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getUserOfRolePage_respone:{}", JSON.toJSONString(userOfRolePage));
			return Lists.newArrayList();
		}
		log.info("ChageeEmpReadApiService_getUserOfRolePage_response：{}", JSONObject.toJSONString(userOfRolePage.getResponseVo().getPageList()));
		return userOfRolePage.getResponseVo().getPageList();

	}


	/**
	 * 查询子公司列表
	 * @return list
	 */
	public List<DeptBO> getSubCompanyList() {
		return queryDeptList(ChageeDeptTypeEnum.CHILD_CORP, null, null, null, false, null);
	}

	/**
	 * 根据userId查询用户信息
	 * @param userId uid
	 * @param isLarkUserId 是否是飞书userId
	 */
	public EmployeeResp getUserByUserId(String userId, boolean isLarkUserId) {
		long userid = 0L;
		try {
			if (!isLarkUserId) {
				userid = Long.parseLong(userId);
			}
		} catch (Exception e) {
			log.error("number format exception", e);
			throw new BizException(BizErrorCode.CHAR_FORMAT_FAIL);
		}
		ChageeUserDetailByIdReq req = new ChageeUserDetailByIdReq();
		if (isLarkUserId) {
			req.setFeiShuUserId(userId);
		} else {
			req.setUserId(userid);
		}
		req.setIsQueryDeptInfo(true);
		return getUserInfo(req);
	}

	/**
	 * 根据userCode查询用户信息
	 * @param userCode userCode
	 */
	public EmployeeResp getUserByUserCode(String userCode) {
		ChageeUserDetailByIdReq req = new ChageeUserDetailByIdReq();
		req.setFeishuEmpNo(userCode);
		req.setIsQueryDeptInfo(true);
		return getUserInfo(req);
	}

	/**
	 * 根据userCode查询用户信息
	 * @param userCode userCode
	 */
	public EmployeeResp getUserByUserCodeSync(String userCode) {
		ChageeUserDetailByIdReq req = new ChageeUserDetailByIdReq();
		req.setFeishuEmpNo(userCode);
		req.setIsQueryDeptInfo(true);
		return getUserInfoSync(req);
	}

	private EmployeeResp getUserInfo(ChageeUserDetailByIdReq req){
		log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChageeUserDetail request:{}", req);
		SoaResponse<EmployeeResp, Void> chageeUserDetail = chageeEmpReadApiService.getChageeUserDetail(req);
		log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChageeUserDetail response:{}", JSON.toJSONString(chageeUserDetail));
		if (SoaUtil.isSucc(chageeUserDetail) && Objects.nonNull(chageeUserDetail.getResponseVo())) {
			return chageeUserDetail.getResponseVo();
		}
		return EMPTY_USER;
	}

	private EmployeeResp getUserInfoSync(ChageeUserDetailByIdReq req){
		log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChageeUserDetail request:{}", req);
		SoaResponse<EmployeeResp, Void> chageeUserDetail = chageeEmpReadApiService.getChageeUserDetail(req);
		log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChageeUserDetail response:{}", JSON.toJSONString(chageeUserDetail));
		if (SoaUtil.isSucc(chageeUserDetail) && Objects.nonNull(chageeUserDetail.getResponseVo())) {
			return chageeUserDetail.getResponseVo();
		}
		return null;
	}


	/**
	 * 查询营运部人员列表
	 * @param companyId companyId
	 * @param officeId officeId
	 * @return list
	 */
	public List<EmployeeResp> queryServiceUserList(String companyId, String officeId){
		List<DeptBO> deptBOS = null;
		// 有办事处id，优先使用办事处id，否则使用子公司
		if (StringUtils.isNotBlank(officeId)) {
			deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, ChageeDeptTagEnum.SERVICE, null, Long.parseLong(officeId), true, null);
		}
		if (StringUtils.isNotBlank(companyId) && (Objects.isNull(deptBOS) || deptBOS.isEmpty())){
			deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, ChageeDeptTagEnum.SERVICE, null, Long.parseLong(companyId), true, null);
		}
		log.info("queryDeptList result = "+ JSON.toJSONString(deptBOS));
		if (deptBOS.isEmpty()) {
			log.info("query dept is empty, companyId:{}, officeId:{}", companyId, officeId);
			return Lists.newArrayList();
		}
		List<Long> deptIds = deptBOS.stream().map(DeptBO::getId).collect(Collectors.toList());
		return batchQueryUserListByDeptIds(deptIds, true, null, null);
	}

	/**
	 * 查询子公司DTC人员列表
	 * @param companyId companyId
	 * @return list
	 */
	public List<EmployeeResp> queryDTCUserList(String companyId) {
		List<DeptBO> deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, ChageeDeptTagEnum.DTC, null, Long.parseLong(companyId), true, null);
		if (deptBOS.isEmpty()) {
			log.info("query DTC dept is empty, companyId:{}", companyId);
			return Lists.newArrayList();
		}
		List<Long> deptIds = deptBOS.stream().map(DeptBO::getId).collect(Collectors.toList());
		return batchQueryUserListByDeptIds(deptIds, false, null, null);
	}


	// 查询总部IT服务组人员列表
	public List<EmployeeResp> queryITUserList() {
		List<DeptBO> deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, ChageeDeptTagEnum.IT, null, null, false, null);
		if (deptBOS.isEmpty()) {
			log.info("query IT dept is empty");
			return Lists.newArrayList();
		}
		List<Long> deptIds = deptBOS.stream().map(DeptBO::getId).collect(Collectors.toList());
		return batchQueryUserListByDeptIds(deptIds, false, null, null);
	}

	/**
	 * 查询营业部负责人
	 * @param companyId
	 * @param officeId
	 * @return
	 */
	public EmployeeResp queryServerGroupDeptLeader(String companyId, String officeId) {
		return queryDeptLeader(ChageeDeptTagEnum.SERVICE, companyId, officeId, null);
	}
	/**
	 * 查询部门负责人
	 * @param deptType 部门类型
	 * @param companyId 子公司id
	 * @param officeId 办事处id
	 * @return user
	 */
	public EmployeeResp queryDeptLeader(ChageeDeptTagEnum deptType, String companyId, String officeId, String tagCode) {
		List<DeptBO> deptBOS = null;
		// 有办事处id，优先使用办事处id，否则使用子公司
		if (StringUtils.isNotBlank(officeId)) {
			deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, deptType, null, Long.parseLong(officeId), true, tagCode);
		}
		if (StringUtils.isNotBlank(companyId) && (Objects.isNull(deptBOS) || deptBOS.isEmpty())){
			deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, deptType, null, Long.parseLong(companyId), true, tagCode);
		}
		if (CollUtil.isEmpty(deptBOS)) {
			return EMPTY_USER;
		}
		if (CollUtil.isNotEmpty(deptBOS)&&deptBOS.size() > 1) {
			log.info("Multiple departments were queried, deptType:{}, companyId:{}, officeId:{}", deptType.getDesc(), companyId, officeId);
		}
		DeptBO deptBO = deptBOS.get(0);
		return getUserByUserId(deptBO.getLeaderUserId(), false);
	}

	/**
	 * 批量查询部门下人员列表
	 * @param deptIds deptIds
	 * @param fetchChild bool
	 * @param userName userName
	 * @param phone phone
	 * @return list
	 */
	public List<EmployeeResp> batchQueryUserListByDeptIds(List<Long> deptIds, Boolean fetchChild, String userName, String phone) {
		if (deptIds.isEmpty()) {
			return Lists.newArrayList();
		}
		List<EmployeeResp> result = new ArrayList<>();
		for (Long deptId : deptIds) {
			result.addAll(queryUserListOnDept(deptId, fetchChild, userName, phone));
		}
		return result;
	}

	/**
	 * 根据姓名模糊查询人员列表
	 * @param userName
	 * @return
	 */
	public PaginationData<EmployeeResp> queryUserListbyUserName(String userName, Integer pageNum, Integer pageSize) {
		EmployeeQueryReq req = new EmployeeQueryReq();
		req.setPageNum(pageNum);
		req.setPageSize(pageSize);
		req.setChageeUserName(userName);
		SoaResponse<PaginationData<EmployeeResp>, Void> chageeUserList = chageeEmpReadApiService.getChageeUserList(req);
		log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.queryUserListbyUserName_result:{}",chageeUserList);
		if (!SoaUtil.isSucc(chageeUserList)) {
			throw new BizException(chageeUserList.getReturnCode(), chageeUserList.getReturnMsg());
		}
		PaginationData<EmployeeResp> responseVo = chageeUserList.getResponseVo();

		return responseVo;
	}

	/**
	 * 查询部门下全部人员列表
	 * @param deptId 部门id
	 * @return list
	 */
	public List<EmployeeResp> queryUserListOnDept(Long deptId, Boolean fetchChild, String userName, String phone) {
		EmployeeQueryReq req = new EmployeeQueryReq();
		req.setPageNum(DEFAULT_PAGE_NUM);
		req.setPageSize(DEFAULT_PAGE_SIZE);
		req.setChageeOrgId(deptId);
		req.setChageeUserName(userName);
		req.setPhoneNumber(phone);
		req.setFetchChild(fetchChild);
		req.setIsQueryDeptInfo(true);
		SoaResponse<PaginationData<EmployeeResp>, Void> chageeUserList;
		try {
			chageeUserList = chageeEmpReadApiService.getChageeUserList(req);
		} catch (Exception e) {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChageeUserList throw excepton." + e.getMessage());
			return Lists.newArrayList();
		}
		if (!SoaUtil.isSucc(chageeUserList) || Objects.isNull(chageeUserList.getResponseVo()) ||
			Objects.isNull(chageeUserList.getResponseVo().getPageList())) {
			log.info("com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService.getChageeUserList respone:{}", JSON.toJSONString(chageeUserList));
			return Lists.newArrayList();
		}

		return chageeUserList.getResponseVo().getPageList();
	}

	/**
	 * 查询部门列表
	 */
	public List<DeptBO> queryDeptList(ChageeDeptTypeEnum deptType, ChageeDeptTagEnum deptTag, String regionCode, Long parentOrgId, Boolean fetchChild, String tagCode) {
		OrgQueryReq req = new OrgQueryReq();
		req.setPageNum(DEFAULT_PAGE_NUM);
		req.setPageSize(DEFAULT_PAGE_SIZE);
		req.setParentOrgId(parentOrgId);
		req.setRegionCode(regionCode);
		req.setChageeDeptTagEnum(deptTag);
		req.setChageeDeptTypeEnum(deptType);
		req.setFetchChild(fetchChild);
		req.setTagCode(tagCode);
		SoaResponse<PaginationData<DeptBO>, Void> chageeOrgList;
		try {
			chageeOrgList = chageeOrgReadApiService.getChageeOrgList(req);
		} catch (Exception e) {
			log.info("com.chagee.personnel.api.event.service.dept.ChageeOrgReadApiService.getChageeOrgList throw exception. " + e.getMessage());
			return Lists.newArrayList();
		}
		if (!SoaUtil.isSucc(chageeOrgList) || Objects.isNull(chageeOrgList.getResponseVo()) ||
			Objects.isNull(chageeOrgList.getResponseVo().getPageList())) {
			log.info("com.chagee.personnel.api.event.service.dept.ChageeOrgReadApiService.getChageeOrgList call fail or responseVo is null.");
			return Lists.newArrayList();
		}
		return chageeOrgList.getResponseVo().getPageList();
	}

	/**
	 * 查询子公司数据
	 * @param deptId
	 */
	public DeptBO queryDeptById(String deptId, String deptCode) {
		if (StringUtils.isBlank(deptId) && StringUtils.isBlank(deptCode)) {
			log.info("query dept detail by id request param deptId and deptCode can not all empty.");
			return EMPTY_DEPT;
		}

		OrgSingleReq req = new OrgSingleReq();
		req.setDeptId(StringUtils.isBlank(deptId) ? null : Long.parseLong(deptId));
		req.setDeptCode(deptCode);
		SoaResponse<DeptBO, Void> chageeOrgDetail;
		try {
			chageeOrgDetail = chageeOrgReadApiService.getChageeOrgDetail(req);
			log.info("call chageeOrgReadApiService.getChageeOrgDetail response:{}", JSON.toJSONString(chageeOrgDetail));
		} catch (Exception e) {
			log.error("call chageeOrgReadApiService.getChageeOrgDetail throw exception.", e);
			return EMPTY_DEPT;
		}
		if (!SoaUtil.isSucc(chageeOrgDetail) || Objects.isNull(chageeOrgDetail.getResponseVo())) {
			log.info("call chageeOrgReadApiService.getChageeOrgDetail fail. response:{}", JSON.toJSONString(chageeOrgDetail));
			return EMPTY_DEPT;
		}

		return chageeOrgDetail.getResponseVo();
	}

	/**
	 * 批量查询用户信息
	 * @param userIds
	 * @return
	 */
	public List<EmployeeResp> getChageeUserDetailListByLarkUserIds(Collection<String> userIds){
		if (CollectionUtils.isEmpty(userIds)) {
			return Collections.emptyList();
		}
		EmployeeQueryByIdReq req = new EmployeeQueryByIdReq();
		req.setFeiShuUserIds(userIds.stream().distinct().collect(Collectors.toList()));
		SoaResponse<List<EmployeeResp>, Void> response = chageeEmpReadApiService.getChageeUserDetailList(req);
		return SoaUtil.unpack(response);
	}

	/**
	 * 批量查询用户信息 用户id
	 * @param userIds
	 * @return
	 */
	public List<EmployeeResp> getChageeUserDetailListByUserIds(Collection<Long> userIds){
		if (CollectionUtils.isEmpty(userIds)) {
			return Collections.emptyList();
		}
		EmployeeQueryByIdReq req = new EmployeeQueryByIdReq();
		req.setUserIds(userIds.stream().distinct().collect(Collectors.toList()));
		SoaResponse<List<EmployeeResp>, Void> response = chageeEmpReadApiService.getChageeUserDetailList(req);
		return SoaUtil.unpack(response);
	}

	/**
	 * 查询子公司 hrbp
	 * @param companyId companyId
	 * @return list
	 */
	public List<EmployeeResp> queryHrbpUserInfo(String companyId) {
		Long companyIdLongType;
		try {
			companyIdLongType = Long.parseLong(companyId);
		} catch (Exception e) {
			log.info("queryHrbpUserInfo string parse long throw exception.", e);
			return Lists.newArrayList();
		}
		QueryDeptRequest request = new QueryDeptRequest();
		request.setId(companyIdLongType);
		request.setFetchChild(false);
		try {
			SoaResponse<DeptHrbpResp, Void> soaResponse = deptApiService.queryDeptHrbpApi(request);
			log.info("deptApiService.queryDeptHrbpApi request:{}, response:{}", companyId, JSON.toJSONString(soaResponse));

			if (SoaUtil.isSucc(soaResponse) && Objects.nonNull(soaResponse.getResponseVo())) {
				DeptHrbpResp responseVo = soaResponse.getResponseVo();
				String[] hrbp = responseVo.getHrbp();
				if (Objects.isNull(hrbp) || hrbp.length < 1) {
					return Lists.newArrayList();
				}
				ArrayList<EmployeeResp> list = new ArrayList<>();
				for (String larkNo : hrbp) {
					EmployeeResp userByUserId = getUserByUserId(larkNo, true);
					if (StringUtils.isBlank(userByUserId.getPhone())) {
						continue;
					}
					list.add(userByUserId);
				}
				return list;
			}
		} catch (Exception e) {
			log.info("call deptApiService.queryDeptHrbpApi throw exception.", e);
		}
		return Lists.newArrayList();
	}


	// 校验打标逻辑
	public HashMap<String, String> checkSignProcessIsSuccess() {
		HashMap<String, String> map = new HashMap<>();
		List<EmployeeResp> employeeResps = queryITUserList();
		String IT = "";
		if (employeeResps.size() > 0) {
			List<String> collect = employeeResps.stream().map(EmployeeResp::getName).collect(Collectors.toList());
			IT = Joiner.on(",").join(collect);
		}
		map.put("IT_SERVER", employeeResps.size() > 0 ? IT : "empty");
		List<DeptBO> subCompanyList = getSubCompanyList();
		for (DeptBO deptBO : subCompanyList) {
			HashMap<String, String> map1 = checkSignProcessByCompanyId(String.valueOf(deptBO.getId()));
			map.put(deptBO.getId()+"_"+deptBO.getDeptName(), map1.get(String.valueOf(deptBO.getId())));
		}
		return map;
	}

	// 校验打标逻辑
	public HashMap<String, String> checkSignProcessByCompanyId(String companyId) {
		HashMap<String, String> map = new HashMap<>();
		StringBuilder builder = new StringBuilder();
		// DTC
		List<EmployeeResp> employeeResps = queryDTCUserList(companyId);
		builder.append("DTC:").append(employeeResps.size() > 0 ? Joiner.on(",").join(employeeResps.stream().map(EmployeeResp::getName).collect(Collectors.toList())) : "empty").append(";");
		try {
			EmployeeResp employeeResp = queryServerGroupDeptLeader(companyId, "");
			builder.append("ServerLeader:").append(StringUtils.isNotBlank(employeeResp.getName()) ? employeeResp.getName() : "empty").append(";");
		} catch (Exception e) {
			builder.append("ServerLeader:").append(e.getMessage()).append(";");
		}

		List<EmployeeResp> sul = queryServiceUserList(companyId, "");
		builder.append("serverUserList:").append(sul.size() > 0 ? Joiner.on(",").join(sul.stream().map(EmployeeResp::getName).collect(Collectors.toList())) : "empty").append(";");
		try {
			List<EmployeeResp> employeeResps1 = queryHrbpUserInfo(companyId);
			builder.append("HRBP:").append(employeeResps1.size() > 0 ? Joiner.on(",").join(employeeResps1.stream().map(EmployeeResp::getName).collect(Collectors.toList())) : "empty").append(";");
		} catch (Exception e) {
			builder.append("HRBP:").append(e.getMessage()).append(";");
		}
		try {
			List<EmployeeResp> employeeResps1 = queryO2OUserList(companyId);
			builder.append("O2O:").append(employeeResps1.size() > 0 ? Joiner.on(",").join(employeeResps1.stream().map(EmployeeResp::getName).collect(Collectors.toList())) : "empty").append(";");
		} catch (Exception e) {
			builder.append("O2O:").append(e.getMessage()).append(";");
		}
		map.put(companyId, builder.toString());

		return map;
	}

	/**
	 * 删除门店
	 * @param storeNo
	 */
	public void delStoreDept(String storeNo) {
		DelDeptReq delDeptReq = new DelDeptReq();
		delDeptReq.setStoreNo(storeNo);
		try {
			SoaResponse<Boolean, Void> soaResponse = chageeDeptStoreApiService.delDeptByStoreNo(delDeptReq);
			log.info("chageeDeptStoreApiService.delDeptByStoreNo [{}] response : {}", storeNo, JSON.toJSONString(soaResponse));
		} catch (Exception e) {
			log.error("chageeDeptStoreApiService.delDeptByStoreNo [{}] throw exception.", storeNo, e);
		}

	}

	/**
	 * 查询门店对应分公司O2O人员手机号
	 * @author yangyang
	 * @date 2024/3/8 17:17
	 * @return java.util.List<com.chagee.personnel.api.event.response.EmployeeResp>
	 */
	public List<EmployeeResp> queryO2OUserList(String companyId) {
		List<DeptBO> deptBOS = queryDeptList(ChageeDeptTypeEnum.DEPT, ChageeDeptTagEnum.O2O, null, Long.parseLong(companyId), true, null);
		if (deptBOS.isEmpty()) {
			log.info("query O2O dept is empty");
			return Lists.newArrayList();
		}
		List<Long> deptIds = deptBOS.stream().map(DeptBO::getId).collect(Collectors.toList());
		return batchQueryUserListByDeptIds(deptIds, false, null, null);
	}

	/**
	 * 查询子公司HRBP人员列表
	 * @param companyId companyId
	 * @return list
	 */
	public List<EmployeeResp> queryHRBPUserList(String companyId) {
		QueryDeptRequest queryDeptRequest = new QueryDeptRequest();
		queryDeptRequest.setId(Long.valueOf(companyId));
		queryDeptRequest.setFetchChild(true);
		SoaResponse<DeptHrbpResp, Void> HRBPResponse = deptApiService.queryDeptHrbpApi(queryDeptRequest);
		DeptHrbpResp hr = SoaUtil.unpack(HRBPResponse);
		Set<String> hrIds = Arrays.stream(hr.getHrbp()).collect(Collectors.toSet());
		if (CollectionUtils.isEmpty(hrIds)) {
			return Collections.emptyList();
		}
		return getChageeUserDetailListByLarkUserIds(hrIds);
	}

	public List<DeptBO> queryDeptList(OrgQueryReq param) {
		SoaResponse<PaginationData<DeptBO>, Void> chageeOrgList = chageeOrgReadApiService.getChageeOrgList(param);
		return  SoaUtil.unpack(chageeOrgList).getPageList();
	}

	/**
	 * 查询部门下全部人员列表
	 * @return list
	 */
	public List<EmployeeResp> queryUserListOnDept(EmployeeQueryReq param) {
		SoaResponse<PaginationData<EmployeeResp>, Void> chageeUserList = chageeEmpReadApiService.getChageeUserList(param);
		return SoaUtil.unpack(chageeUserList).getPageList();
	}

	public List<EmployeeResp> queryUserByTag(String companyId, ChageeDeptTagEnum tag) {
		OrgQueryReq orgQueryReq = new OrgQueryReq();
		orgQueryReq.setChageeDeptTagEnum(tag);
		orgQueryReq.setChageeDeptTypeEnum(ChageeDeptTypeEnum.DEPT);
		orgQueryReq.setParentOrgId(Long.parseLong(companyId));
		orgQueryReq.setFetchChild(true);
		List<DeptBO> deptBOS = queryDeptList(orgQueryReq);
		if (CollectionUtils.isEmpty(deptBOS)) {
			log.info("query_dept_is_empty, companyId:{}", companyId);
			return Collections.emptyList();
		}
		List<Long> deptIds = deptBOS.stream().map(DeptBO::getId).collect(Collectors.toList());
		EmployeeQueryReq employeeQueryReq = new EmployeeQueryReq();
		employeeQueryReq.setChageeOrgIds(deptIds);
		employeeQueryReq.setIsQueryDeptInfo(false);
		employeeQueryReq.setPageSize(DEFAULT_PAGE_SIZE);
		return queryUserListOnDept(employeeQueryReq);
	}
}
